package com.example.swimmingpool.controller;

import cn.hutool.core.lang.UUID;
import com.example.swimmingpool.response.CommonResponse;
import com.example.swimmingpool.util.MinioUtil;
import io.lettuce.core.protocol.Command;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class MinioController {
    @Resource
    private MinioUtil minioUtil;
    @PostMapping("/upload/avatar")
    public Map<String,Object> upLoadAvatar(@RequestParam("file") MultipartFile image) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        var name = UUID.fastUUID().toString(true) ;
        var prefix = Objects.requireNonNull(image.getOriginalFilename()).substring(image.getOriginalFilename().lastIndexOf(".")+1);
        var pictureName = name+"."+prefix;
        minioUtil.put(pictureName,image.getInputStream(),"image/jpeg");
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> resp = new HashMap<>();
        resp.put("url",String.format("%s%s%s","http://116.63.167.105:9000/","swimming-pool/",pictureName));
        map.put("response",resp);
        map.put("url",resp.get("url"));
        map.put("name",pictureName);
        return map;
    }


}
